﻿using System.Collections.Generic;
using System.Linq;
using GTAuto.DAL.Entities;


namespace GTAuto.DAL.DAL
{
    public class DALModelModification : DALBase
    {
        public static DALModelModification Singleton = new DALModelModification();

        public GTModelModification Add(GTModelModification gtBrandModelModification)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                
                var modelMod = new ModelModification();
                modelMod.Name = gtBrandModelModification.Name;
                modelMod.ModelId = gtBrandModelModification.ModelId;
                modelMod.Active = gtBrandModelModification.Active;
                modelMod.LogoPath = gtBrandModelModification.LogoPath;
                modelMod.SmallLogoPath = gtBrandModelModification.SmallLogoPath;


                db.ModelModifications.InsertOnSubmit(modelMod);
                db.SubmitChanges();
                gtBrandModelModification.Id = modelMod.Id;
                return gtBrandModelModification;
            }
        }

        public GTModelModification Update(GTModelModification gtBrandModelModification)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var modelMod = db.ModelModifications.SingleOrDefault(x => x.Id == gtBrandModelModification.Id);
                modelMod.Name = gtBrandModelModification.Name;
                modelMod.ModelId = gtBrandModelModification.ModelId;
                modelMod.Active = gtBrandModelModification.Active;
                modelMod.LogoPath = gtBrandModelModification.LogoPath;
                modelMod.SmallLogoPath = gtBrandModelModification.SmallLogoPath;


                db.SubmitChanges();
                return gtBrandModelModification;
            }
        }


        public GTModelModification Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.ModelModifications.SingleOrDefault(x => x.Id == id);
                GTModelModification gtBrand = new GTModelModification();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTModelModification> GetAll(int modelId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.ModelModifications
                 where p.ModelId==modelId
                 orderby p.Name
                 select p;
                
                var brands = brds.ToList();

               

                var ret = new List<GTModelModification>();
                foreach (var brand in brands)
                {
                    GTModelModification gtBrand = new GTModelModification();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.ModelModifications.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.ModelModifications.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }

    }
}
